package com.bbs.toutiao.service.admin.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.bbs.toutiao.aspect.SysLog;
import com.bbs.toutiao.entity.UserEntity;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.mapper.admin.UserMapper;
import com.bbs.toutiao.service.admin.UserEditorService;
import com.bbs.toutiao.vo.comm.PageVo;
import com.bbs.toutiao.vo.user.UpdateStatusVo;
import com.bbs.toutiao.vo.user.UserRequestPageVo;
import com.bbs.toutiao.vo.user.UserResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户编辑，服务层
 * 描述:
 * 作者： zhangBin
 * 时间： 2023/9/19 14:29
 * 版本： 1.0
 */
@Slf4j
@Service
public class UserEditorImpl implements UserEditorService {
    @Resource
    UserMapper userMapper;


    @SysLog("用户查询个人信息")
    @Override
    public UserEntity getUserData(Integer id) {
        return userMapper.userInfoById(id);
    }


    /**
     * 更新用户状态
     * @param statusVo
     * @return true，false
     */
    @SysLog("管理员更新用户状态")
    @Override
    public Boolean updateStatus(UpdateStatusVo statusVo) {
        DateTime now = DateUtil.date();
        // 时间推移
        DateTime newDate = now.offset(DateField.DAY_OF_YEAR, 7);
        if (statusVo.getStatus()==1){
            statusVo.setBanEndTime(newDate.toString());
        }
        Integer i = userMapper.updateStatus(statusVo);

        return i > 0;
    }

    /**
     * 动态查询用户
     * @param searchVo
     * @return list
     */
    @Override
    public PageVo<UserResponseVo> userSearchPage(UserRequestPageVo searchVo) {
        List<UserResponseVo>  userEntityList = userMapper.findByUserSearchPage(searchVo);
        for (UserResponseVo e : userEntityList){
            int i = userMapper.favoritesCount(e.getId());
            e.setFavorites(i);
        }
        long i = userMapper.findUserCount(searchVo);//数据总数
        if (i==0){
            throw new BaseException(ExceptionEnum.NOT_FOUND);
        }
        PageVo<UserResponseVo> userPageVo = new PageVo<>();
        userPageVo.setPageList(userEntityList);
        userPageVo.setTotal(i);
        return userPageVo;
    }

    /**
     * 动态更新用户信息
     * @param user
     * @return
     */
    @SysLog("更新个人信息")
    @Transactional
    @Override
    public int updateUserInfoById(UserEntity user,String token) {
        int userId;
        try {
            if (ObjUtil.isAllEmpty(user)) {
               throw new BaseException(ExceptionEnum.NOT_NULL);//判断是否全部为空
            }
            JWT jwt = JWTUtil.parseToken(token);
            userId = Integer.parseInt(jwt.getPayload("userId").toString());
        } catch (Exception e) {
            throw new BaseException(ExceptionEnum.TOKEN_FONT_ERROR);
        }
        //判断该邮箱是否被注册
        if (userMapper.findUserByEmail(user.getUserEmail())>0){
            throw new BaseException(400,"邮箱已被注册");
        }
        user.setId(userId);
//        System.out.println("跟新后的用户信息为："+user);
        return userMapper.updateUserInfoById(user);
    }
}
